package com.common.exception;

import com.common.util.PRINT;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import java.sql.SQLException;

/**
 * @ClassName RuntimeExceptionHandler
 * @Description TODO
 * @Author 无名小卒
 * @Date 2023/7/12 19:47
 * @Version 1.0
 */
@ControllerAdvice
public class RuntimeExceptionHandler {
    @ExceptionHandler({RuntimeException.class})
    @ResponseBody
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public ResponseEntity handler(RuntimeException e) {
        pringError(e);
        return ResponseEntity.status(HttpStatus.FORBIDDEN).body(e.getMessage());
    }
    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseBody
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public ResponseEntity paramErrorHandler(MethodArgumentNotValidException e) {
        pringError(e);
        BindingResult bindingResult = e.getBindingResult();
        return ResponseEntity.internalServerError().body(bindingResult.getFieldError().getDefaultMessage());
    }
    @ExceptionHandler({SQLException.class})
    @ResponseBody
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public ResponseEntity handler(SQLException e) {
        pringError(e);
        return ResponseEntity.internalServerError().body(e.getMessage());
    }

    /**
     * 权限认证失败
     * @param e
     * @return
     */
    @ExceptionHandler(AccessDeniedException.class)
    @ResponseBody
    @ResponseStatus(HttpStatus.FORBIDDEN)
    public ResponseEntity<String> TpxtExceptionHandler(AccessDeniedException e) throws AccessDeniedException {
        pringError(e);
        return ResponseEntity.status(HttpStatus.FORBIDDEN).body("没有相应权限");
    }
    @ExceptionHandler(CourseException.class)
    @ResponseBody
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public ResponseEntity TpxtExceptionHandler(CourseException e) {
        pringError(e);
        return ResponseEntity.internalServerError().body(e.getMessage());
    }
    /**
     * 打印异常
     * @param e
     */
    private void pringError(Exception e) {

        StringBuilder str=new StringBuilder();
        str.append("\r\n").append("<<========================异常信息打印开始=========================>>").append("\r\n")
                .append("异常原因: ").append(e.toString()).append("\r\n")
                .append("异常路径：");
        StackTraceElement[] stacks = e.getStackTrace();
        if(stacks!=null) {
            for (int i = 0; i < stacks.length; i++) {
                if (stacks[i].toString().contains("com.")&&stacks[i].toString().contains(".java:")) {
                    str.append(stacks[i]).append("\r\n");
                }
            }
        }else{
            str.append("\r\n");
        }
        str.append("<<========================异常信息打印结束=========================>>");
        PRINT.PN_ERROR_LOG(str.toString());
    }

}

